/**
* Copyright 2012 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.client.ui.form;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.user.client.ui.Widget;
import com.nabla.wapp.client.general.Assert;
import com.nabla.wapp.client.ui.IHasWidgets;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.FormItemIcon;


public class FormItemIconList extends Widget implements IHasWidgets {

	private FormItem				buddy;
	private List<FormItemIcon>		children = new LinkedList<FormItemIcon>();

	public FormItem getBuddy() {
		if (children != null) {
			if (buddy != null && !children.isEmpty())
				buddy.setIcons(children.toArray(new FormItemIcon[0]));
			children = null;
		}
		return buddy;
	}

	@Override
	public void add(Widget w) {
		w = Helper.onCreate(w);

		if (w instanceof IFormItem<?>)
			buddy = ((IFormItem<?>)w).getImpl();
		else {
			Assert.state(w instanceof IFormItemIcon);
			children.add(((IFormItemIcon)w).getImpl());
		}
	}

	@Override
	public void clear() {}

	@Override
	public Iterator<Widget> iterator() {
		return null;
	}

	@Override
	public boolean remove(@SuppressWarnings("unused") Widget w) {
		return false;
	}

}
